//
//  PPEbookPreviewCollectionViewController.m
//  pickpic
//
//  Created by 胡文广 on 2017/4/16.
//  Copyright © 2017年 MicroDream. All rights reserved.
//

#import "PPEbookPreviewCollectionViewController.h"
#import "PPEbookPreviewCell.h"
#import "PPPhoto.h"
@interface PPEbookPreviewCollectionViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView *mainCollectionView;

//@property (nonatomic,strong) NSMutableArray *smallPhotos;
@end

@implementation PPEbookPreviewCollectionViewController
static NSString *const ebookID = @"EbookCell";

//- (NSMutableArray *)smallPhotos{
//
//    if (!_smallPhotos) {
//        _smallPhotos = [NSMutableArray array];
//    }
//    return _smallPhotos;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    for (int i = 0; i<self.photos.count; i++) {
//        if (i >= 1 && i!=self.photos.count-1) {
//            PPPhoto *p = self.photos[i];
//            if (!p.desc.length) {
//                [self.smallPhotos addObject:self.photos[i]];
//            }
//        }
//    }
    
    
    [self creatCollectionView];
    
}
//创建collectionView
-(void)creatCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //该方法也可以设置itemSize
    CGFloat w = (SCREEN_WIDTH-3)/4;
    layout.itemSize =CGSizeMake(w,w);
    // 设置最小行间距
    layout.minimumLineSpacing = 1;
    // 设置垂直间距
    layout.minimumInteritemSpacing = 1;
//    2.初始化collectionView
    _mainCollectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:_mainCollectionView];
    
    [_mainCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PPEbookPreviewCell class]) bundle:nil] forCellWithReuseIdentifier:ebookID];
    
    _mainCollectionView.delegate = self;
    _mainCollectionView.dataSource = self;
    
    _mainCollectionView.backgroundColor = WHITE;
    
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    self.mainCollectionView.frame = self.view.bounds;
}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    CGFloat width = (collectionView.width - 3)/4;
//    
//    return CGSizeMake(width, width);
//}
//
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
//    return 1;
//}
//
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//    return 1;
//}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PPEbookPreviewCell *cell = (PPEbookPreviewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ebookID forIndexPath:indexPath];
    PPPhoto *p = self.photos[indexPath.item];
    cell.img.contentMode = UIViewContentModeScaleAspectFill;
    [cell.img sd_setImageWithURL:[NSURL URLWithString:p.bigImgUrl]];
    return cell;
}

//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
//    PPPhoto *p = self.photos[indexPath.item];
//    PPPhoto *p = self.smallPhotos[indexPath.item];
    
//    PPEbookPreviewCell *cell = (PPEbookPreviewCell*)[collectionView cellForItemAtIndexPath:indexPath];
//    p.sourceImageView = cell.img;
//    
//    [self.photos replaceObjectAtIndex:indexPath.item+2 withObject:p];
    
    if ([self.delegate respondsToSelector:@selector(ebookPreviewCollectionView:didSelIndex:photos:)]) {
        [self.delegate ebookPreviewCollectionView:collectionView didSelIndex:indexPath photos:self.photos];
    }
}
@end
